﻿
//需要保存的对象;对象模式{table:"tablename",data:data,type:'delete|insert|update'}; 
var _savedata = [];
function fnSaveData(tablename, data, type) {

    //console.log("+++++++++++++++++++++++++++fnSaveData");
    //console.log("tablename:" + tablename);
    //console.log(isArrayFn(data));
    //console.log(data);
    //console.log("+++++++++++++++++++++++++++fnSaveData");

    //非数组对象转换为数组
    if (!isArrayFn(data)) {
        var arr = new Array();
        arr.push(data);
        data = arr;
    }
    
    Enumerable.From(data).ForEach(function (x) {
        var dataarray = new Array();
        dataarray.push(x);
        var entity = { tablename: tablename, data: mini.encode(dataarray), type: type };
        _savedata.push(entity);
    });

}

function isArrayFn(value) {
    if (typeof Array.isArray === "function") {
        return Array.isArray(value);
    } else {
        return Object.prototype.toString.call(value) === "[object Array]";
    }
}

//将数据保存到数据库
//checkdataclassname 数据校验类名
function fnDataSaveToDataBase(checkdataclassname, id) {
    
    var json = mini.encode(_savedata);
    var msg = mini.loading("数据处理中，请稍后......", "系统消息提醒");
    $.ajax({
        url: "/DataTableSave/Save",
        type: "post",
        data: { data: json, checkdataclassname: checkdataclassname },
        cache: false,
        success: function (text) {
            var result = mini.decode(text);
            var message = result.MessageInfo;
            if (result.Stauts == 1) {
                mini.hideMessageBox(msg);
                mini.showMessageBox({
                    showModal: false,
                    width: 250,
                    title: "保存成功提示信息",
                    message: message,
                    buttons: [],
                    iconCls: "mini-messagebox-ok",
                    timeout: 2000
                });
               
                SetData({ id: id ,operator_type:1});
              
            }
            if (result.Stauts == 0) {
                var message = result.MessageInfo;
                mini.showMessageBox({
                    showModal: true,
                    width: 850,
                    height: 250,
                    title: "保存失败提示信息",
                    message: message,
                    buttons: [],
                    iconCls: "mini-messagebox-ok"
                });
                if (mini.get("btnQpDownload") != null) {
                    mini.get("btnQpDownload").setEnabled(true);
                }
            }
           
           
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Error" + jqXHR.responseText);
        }
    });
    _savedata = Enumerable.From(_savedata).Where(function (x) {
        return x.type == "delete";
    }).ToArray();
    mini.hideMessageBox(msg);
   
}  